﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Ooaavee.Scripting;

namespace DevelopersTestPad.Worksheets.DotNet
{
    /// <summary>
    /// Displays compiler errors.
    /// </summary>
    internal partial class CompilerErrorsSubView : SubView
    {
        public CompilerErrorsSubView()
        {
            InitializeComponent();

            AutoFitListView();
        }

        /// <summary>
        /// Clears the compiler errors sub view.
        /// </summary>
        public void Clear()
        {
            listView.Items.Clear();
            AutoFitListView();
        }

        /// <summary>
        /// Adds a compile exception into the compiler errors sub view.
        /// </summary>
        /// <param name="exception">The compile exception to add.</param>
        public void Add(ScriptCompileException exception)
        {
            Clear();

            // Show compile errors.
            foreach (ScriptCompileException.ErrorMessage error in exception.ErrorMessages)
            {
                ListViewItem item = new ListViewItem { Text = error.ErrorNumber };
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = error.ErrorText });
                listView.Items.Add(item);
            }

            // Auto fit the ListView control.
            AutoFitListView();
        }

        /// <summary>
        /// Auto fits the ListView control.
        /// </summary>
        private void AutoFitListView()
        {
            for (int i = 0; i <= listView.Columns.Count - 1; i++)
            {
                listView.Columns[i].Width = -2;   
            }
        }

        #region ContextMenu

        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            mniClearAll.Enabled = listView.Items.Count > 0;
        }

        /// <summary>
        /// Clear all compiler errors.
        /// </summary>
        private void mniClearAll_Click(object sender, EventArgs e)
        {
            Clear();
        }

        #endregion

    }
}
